package com.ebupt.migu.music.propagate.controller;

import cn.hutool.core.date.DateUtil;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.ExcelUtils;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.common.util.excelSheet.Sheet;
import com.ebupt.migu.music.propagate.service.PropagateEffectSerivce;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/propagate/effect")
public class PropagateEffectController {


    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private PropagateEffectSerivce propagateEffectSerivce;


    private static String[] propagateArr = {"createTime", "executeTime", "strategyId", "strategyName","succeed","fail"};
    private static String[] propagateHead = {"策略创建时间", "策略执行时间", "策略ID", "策略名称","下发成功数","下发失败数"};



    /**
     * 查询策略效果统计列表
     *
     * @return
     */
    @RequestMapping("/list")
    public ResultObject queryApproveList() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        return propagateEffectSerivce.queryList(params);
    }




    /**
     * 导出效果统计
     * @return
     */
    @RequestMapping(value = "/export", produces = MediaType.TEXT_HTML_VALUE)
    public void export(){
        Map<String,Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        List<Map<String, Object>> list = propagateEffectSerivce.export(params);
        list.stream().map(map -> {
            map.put("createTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(map.get("createTime")));
            map.put("executeTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(map.get("executeTime")));
            return map;
        }).collect(Collectors.toList());
        ExcelUtils excel = new ExcelUtils();
        Sheet sheet = new Sheet();
        excel.setName("propagate_total").setVersion("2007");
        sheet.setName("宣发效果统计");
        sheet.setHead(propagateHead);
        sheet.fillData(list, propagateArr);
        excel.addSheet(sheet);
        excel.zipAndResponse(response, request);
    }

}
